<?php


namespace App\HttpController;


use App\Model\UserModel;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;
use EasySwoole\ORM\DbManager;

/**
 * 测试
 *
 * @package App\HttpController
 */
class Test extends Controller
{
    /**
     * 获取redis的配置文件
     *
     * @throws \Throwable
     */
    public function config()
    {
        ## 正确
        // 取出连接池管理对象，然后获取连接对象（getObject）
        try {
            $instance = \EasySwoole\EasySwoole\Config::getInstance();
            $config   = $instance->getConf('MYSQL');
            var_dump($config);

        } catch (\Throwable $e) {

        }
        ## 正确
    }

    public function mysqlpool()
    {
        try {
            /** @var \EasySwoole\ORM\Db\Connection $connection **/
            $connection = \EasySwoole\ORM\DbManager::getInstance()->getConnection();

            /** @var \EasySwoole\ORM\Db\Connection $connection **/
            // $connection->defer();
            // $connection->defer(3.0);

            // 事务
            /*\EasySwoole\ORM\DbManager::getInstance()->startTransaction();
            \EasySwoole\ORM\DbManager::getInstance()->commit();
            \EasySwoole\ORM\DbManager::getInstance()->rollback();
            \EasySwoole\ORM\DbManager::getInstance()->invoke(function (\EasySwoole\ORM\Db\ClientInterface $client){
                \EasySwoole\ORM\DbManager::getInstance()->startTransaction($client);
                \EasySwoole\ORM\DbManager::getInstance()->commit($client);
                \EasySwoole\ORM\DbManager::getInstance()->rollback($client);
            });*/
            $param = $this->request()->getRequestParam();
            $page  = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 20;
            $value = DbManager::getInstance()->invoke(function ($client) use ($page, $param, $limit) {
                $userModel = UserModel::invoke($client);
                $data = $userModel->getAll($page, $param['keyword'] ?? null, $limit);
                return $data;
            });
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }

        $this->writeJson(Status::CODE_OK, $value, 'success');
    }
}